package com.example.canteen.dao.impl;

import com.example.canteen.dao.CanteenStatementDao;
import com.example.canteen.domain.CanteenStatement;
import com.example.canteen.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CanteenStatementDaoImpl implements CanteenStatementDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public CanteenStatement findById(int id) {
        CanteenStatement cantenstatement = null;
        String sql = "SELECT id,book_statement,open_statement FROM canteen_statement WHERE id=?";
        try {
            cantenstatement =template.queryForObject(sql,new BeanPropertyRowMapper<CanteenStatement>(CanteenStatement.class),id);
        }catch (Exception e){

        }
        return cantenstatement;
    }

    @Override
    public CanteenStatement findByBookAndOpen(String book, String open) {
        String sql = "SELECT id,book_statement,open_statement FROM canteen_statement WHERE book_statement=? and open_statement=?";
        CanteenStatement cantenstatement = null;
        try{
            cantenstatement = template.queryForObject(sql,new BeanPropertyRowMapper<CanteenStatement>(CanteenStatement.class),book,open);
        }catch(Exception e){

        }
        return cantenstatement;
    }

    @Override
    public List<CanteenStatement> findAll() {
        String sql = "SELECT id,book_statement,open_statement FROM canteen_statement";
        return template.query(sql, new BeanPropertyRowMapper<CanteenStatement>(CanteenStatement.class));
    }


    @Override
    public int delete(int id) {
        String sql = "DELETE FROM canteen_statement WHERE id=?";
        return template.update(sql,id);
    }

    @Override
    public void save(CanteenStatement canteenStatement) {
        String sql = "insert into canteen_statement(book_statement,open_statement) value(?,?)";
        template.update(sql, canteenStatement.getBookStatement(),
                canteenStatement.getOpenStatement());

    }

    @Override
    public int update(CanteenStatement canteenStatement) {
        String sql = "UPDATE canteen_statement SET book_statement=?,open_statement=? WHERE id=?";
        return template.update(sql,canteenStatement.getBookStatement(),
                canteenStatement.getOpenStatement(),canteenStatement.getId());

    }


}
